package cn.aylog.common.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@ApiModel("通用返回参数")
@Data
public class R {

    @ApiModelProperty("状态码")
    private Integer code;
    @ApiModelProperty("状态信息")
    private String message;
    @ApiModelProperty("是否成功")
    private Boolean success;
    @ApiModelProperty("响应数据")
    private Map<String, Object> data = new HashMap<>();

    private R() {}

    public static R ok() {
        R r = new R();
        r.code = Result.SUCCESS;
        r.success = true;
        r.message = "成功";
        return r;
    }

    public static R error() {
        R r = new R();
        r.code = Result.ERROR;
        r.success = false;
        r.message = "失败";
        return r;
    }

    public R code(Integer code) {
        this.code = code;
        return this;
    }

    public R success(Boolean success) {
        this.success = success;
        return this;
    }

    public R message(String message) {
        this.message = message;
        return this;
    }

    public R data(String key, Object data) {
        this.data.put(key, data);
        return this;
    }

    public R data(Map<String, Object> data) {
        this.data.putAll(data);
        return this;
    }
}
